/**
 * \file ei4.cpp
 * Ei4 executable's main() and general utility functions.
 */
#include <stdarg.h>

#include "include/App.h"
#include "include/Scripting.h"

namespace Ei {

// XXX should live in its own module
Log* eiLog = NULL;
void eilog(const Ogre::String& fmt, ...) {
        va_list args;
        va_start( args, fmt );
        char msg[2048]; // We proudly accept buffer overflows!
        vsprintf( msg, fmt.c_str(), args );

        if (eiLog)
                eiLog->logMessage(String(msg));
        else
                fprintf(stderr,"%s\n", msg);
        va_end( args );
}

} // namespace Ei

extern "C" {
int main(int argc, char **argv)
{
	// prepare logging
        LogManager lm;
        lm.createLog("Ogre.log", true, false);
	Ei::eiLog = lm.createLog("ei.log");

	// parse command line
        std::vector<const char*> script_names;
        const char * scene_name = NULL;
        char ** arg = argv+1;
        while(*arg) {
                String ext(strrchr(*arg,'.'));
                if (ext == ".lua" || ext == ".cfg") {
                        script_names.push_back(*arg);
                } else if (ext == ".OSM" || ext == ".scene") {
                        scene_name = *arg;
                }
                arg++;
        }

	// init scripting
	Ei::Scripting scr(script_names);
        if (!scene_name) scene_name = scr.getGlobalCString("scene");
        scr.saveDefaults(scene_name);

	// run
	Ei::App theApp(&scr,scene_name); 

        return 0;
}

} // extern C

